package com.dlc.yunguang.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.dlc.commonlibrary.utils.ToastUtil;
import com.dlc.commonlibrary.widget.popup.CommonPopupList;
import com.dlc.yunguang.base.BaseFragment;
import com.dlc.yunguang.R;
import com.seuic.uhf.UHFService;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

//import com.google.android.gms.location.LocationClient;

public class FragmentScannerListFragment extends BaseFragment   {

	private View rootView;

	TextView tvHotel;

	FrameLayout dropHotel;
	Button sure;


	private CommonPopupList mHotelPopup;//酒店
	private UHFService mDevice;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Retain this fragment across configuration changes.
		setRetainInstance(true);

		// Put objects which you do not want to be created again.
		// ie. run once.


	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_scanner_list, container, false);

		tvHotel = (TextView) rootView.findViewById(R.id.tv_hotel);
		dropHotel = (FrameLayout) rootView.findViewById(R.id.drop_hotel);
		sure = (Button) rootView.findViewById(R.id.sure);
		try {
			mDevice = UHFService.getInstance();
		}catch (Exception e){}

		mHotelPopup = CommonPopupList.newBuilder(getActivity())
				.setWidthEquals(dropHotel)
				.setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
					@Override
					public void onSelectItem(CommonPopupList popup, String item, int position) {
						popup.dismiss();
						// TODO: 2017/7/20
						tvHotel.setText(item);
						SetPower(item);
					}
				})
				.build();

		List<String> datas = new ArrayList<>();


		for (int i = 15; i <31; i++) {
			datas.add(i+"");
		}
		mHotelPopup.setData(datas);
		GetPower();
		Observable.timer(1, TimeUnit.SECONDS).subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
						.subscribe(new Observer<Long>() {
							@Override
							public void onCompleted() {

							}

							@Override
							public void onError(Throwable throwable) {

							}

							@Override
							public void onNext(Long aLong) {
								GetPower();
							}
						});
		dropHotel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				mHotelPopup.showAsDropDown(view);
			}
		});
		sure.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				ToastUtil.show(getActivity(),"功率设置成功");
			}
		});
		return rootView;
	}
	public void GetPower() {
		if (mDevice != null) {
			int power = mDevice.getPower();
			tvHotel.setText(power + "");
		}
	}
	public void SetPower(String value) {

		if (!value.isEmpty()) {
			int power = Integer.valueOf(value);
			if (mDevice != null) {
				boolean ret = mDevice.setPower(power);
				if (!ret) {
					ToastUtil.show(getActivity(), "功能设置失败");
				}
			}
		} else {
			ToastUtil.show(getActivity(),"功能设置成功");
		}

	}
//	@OnClick({ R.id.check ,R.id.drop_hotel})
//	public void onViewClicked(View view) {
//		switch (view.getId()) {
//			case R.id.check:
//
//
//				break;
//			case R.id.drop_hotel:
//				mHotelPopup.showAsDropDown(view);
//				break;
//		}
//	}
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_scanner_list;
    }

    @Override
	public void onDestroy() {
        super.onDestroy();

    }
	


}
